﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using Demo_G.O.S.E.ServerEngine.Common;
using System.Threading;
using System.Diagnostics;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Map
{
    /// <summary>
    /// 
    /// </summary>
    internal class SectorEnumerator : IPooledEnumerator, IDisposable
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 地图
        /// </summary>
        private BaseMap m_BaseMap = null;
        /// <summary>
        /// 范围
        /// </summary>
        private Rectangle2D m_Bounds = Rectangle2D.ZeroRectangle2D;
        /// <summary>
        /// 地图地点x点的用于计算的点
        /// </summary>
        private long m_xSector = 0;
        /// <summary>
        /// 地图地点y点的用于计算的点
        /// </summary>
        private long m_ySector = 0;
        /// <summary>
        /// 地图地点x点的开始位置
        /// </summary>
        private long m_xSectorStart = 0;
        /// <summary>
        /// 地图地点y点的开始位置
        /// </summary>
        private long m_ySectorStart = 0;
        /// <summary>
        /// 地图地点x点的结束位置
        /// </summary>
        private long m_xSectorEnd = 0;
        /// <summary>
        /// 地图地点y点的结束位置
        /// </summary>
        private long m_ySectorEnd = 0;
        /// <summary>
        /// 类型
        /// </summary>
        private SectorType m_EnumeratorType;
        #endregion

        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseMap"></param>
        /// <param name="bounds2D"></param>
        /// <param name="enumeratorType"></param>
        private SectorEnumerator( BaseMap baseMap, Rectangle2D bounds2D, SectorType sectorEnumeratorType )
        {
            m_BaseMap = baseMap;
            m_Bounds = bounds2D;
            m_EnumeratorType = sectorEnumeratorType;
        }
        #endregion

        #region zh-CHS 共有静态方法 | en Public Static Methods
        #region zh-CHS 私有静态成员变量 | en Private Static Member Variables
        /// <summary>
        /// 
        /// </summary>
        private static Queue<SectorEnumerator> s_InstancePool = new Queue<SectorEnumerator>();
        /// <summary>
        /// 
        /// </summary>
        private static object s_LockInstancePool = new object();
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Map"></param>
        /// <param name="Bounds"></param>
        /// <param name="enumeratorType"></param>
        /// <returns></returns>
        public static SectorEnumerator Instantiate( BaseMap Map, Rectangle2D Bounds, SectorType sectorEnumeratorType )
        {
            SectorEnumerator l_SectorEnumerator = null;

            Monitor.Enter( s_LockInstancePool );
            {
                if ( s_InstancePool.Count > 0 )
                {
                    l_SectorEnumerator = s_InstancePool.Dequeue();

                    l_SectorEnumerator.m_BaseMap = Map;
                    l_SectorEnumerator.m_Bounds = Bounds;
                    l_SectorEnumerator.m_EnumeratorType = sectorEnumeratorType;

                }
            }
            Monitor.Exit( s_LockInstancePool );

            if ( l_SectorEnumerator == null )
                l_SectorEnumerator = new SectorEnumerator( Map, Bounds, sectorEnumeratorType );

            l_SectorEnumerator.Reset();
            return l_SectorEnumerator;
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            while ( true )
            {
                ++m_CurrentIndex;

                if ( m_CurrentIndex >= m_CurrentArray.Length )
                {
                    m_ySector += BaseMap.SectorSize;

                    if ( m_ySector > m_ySectorEnd )
                    {
                        m_ySector = m_ySectorStart;

                        m_xSector += BaseMap.SectorSize;

                        if ( m_xSector > m_xSectorEnd )
                        {
                            m_CurrentIndex = -1;
                            m_CurrentArray = s_ZeroArray;

                            return false;
                        }
                    }

                    m_CurrentIndex = -1;
                    m_CurrentArray = GetArrayForSector( m_BaseMap.GetSector( m_xSector, m_ySector ) );
                }
                else return true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Reset()
        {
            // 确保边界在地图的边界内
            m_BaseMap.Bound( m_Bounds.Start.X, m_Bounds.Start.Y, out m_xSectorStart, out m_ySectorStart );
            m_BaseMap.Bound( m_Bounds.End.X, m_Bounds.End.Y, out m_xSectorEnd, out m_ySectorEnd );

            m_xSector = m_xSectorStart;
            m_ySector = m_ySectorStart;

            m_CurrentIndex = -1;
            m_CurrentArray = GetArrayForSector( m_BaseMap.GetSector( m_xSector, m_ySector ) );
        }

        /// <summary>
        /// 
        /// </summary>
        public void Release()
        {
            if ( m_BaseMap == null )
                return;
            else
            {
                m_BaseMap = null;
                m_Enumerable = null;
            }

            Monitor.Enter( s_LockInstancePool );
            {
                s_InstancePool.Enqueue( this );
            }
            Monitor.Exit( s_LockInstancePool );
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            Release();
        }
        #endregion

        #region zh-CHS 接口实现属性 | en Interface Implementation Properties
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 
        /// </summary>
        private IPooledEnumerable m_Enumerable = null;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public IPooledEnumerable Enumerable
        {
            get { return m_Enumerable; }
            set { m_Enumerable = value; }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 空的数组
        /// </summary>
        private static object[] s_ZeroArray = new object[0];
        /// <summary>
        /// 当前数据列表
        /// </summary>
        private object[] m_CurrentArray = s_ZeroArray;
        /// <summary>
        /// 当前数据列表的索引
        /// </summary>
        private int m_CurrentIndex = -1;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        public object Current
        {
            get { return m_CurrentArray[m_CurrentIndex]; }
        }
        #endregion

        #region zh-CHS 私有方法 | en Private Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sector"></param>
        /// <returns></returns>
        private object[] GetArrayForSector( Sector sector )
        {
            switch ( m_EnumeratorType )
            {
                case SectorType.Clients:
                    return sector.ClientsToArray();
                case SectorType.Mobiles:
                    return sector.MobilesToArray();
                case SectorType.Items:
                    return sector.ItemsToArray();
                default:
                    Debug.WriteLine( "SectorEnumerator.GetArrayForSector(...) - m_EnumeratorType == unknown error(Invalid SectorEnumeratorType!)" );
                    return s_ZeroArray;
            }
        }
        #endregion
    }
}
#endregion

